/*
 * Copyright 2000-2020 ALPHA LAB.Inc All Rights Reserved.
 */
package com.alphalab.framework.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;  
import com.alphalab.framework.domain.ItemCondition;
import com.alphalab.framework.domain.QueryOperatorEnum;
import com.alphalab.framework.domain.QueryOption; 

/**
 * 条件对象转换工具类
 * 将前端传到场景层的ItemCondition对象或对象集合转换为一个QueryOption对象.
 * @author allen 
 * @version 1.0.0 2017年7月11日
 */
public final class ExplainConditionUtil { 
	
    /**
     * 不被实例化.
     */
	private ExplainConditionUtil() { 
		
	} 

	/**
	 * 将查询条件对象转换成QueryOption对象. 
	 * @param filters filters
	 * @return QueryOption
	 */	
	public static QueryOption explainConditionOption(List<ItemCondition> filters) {
		final QueryOption option = new QueryOption();
		final Map param = new HashMap(10);
		final StringBuffer sb = new StringBuffer("");
		if ((filters == null) || (filters.size() < 1)) {
			option.setHql(sb.toString());
			option.setParam(param);
			return option;
		}
		for (int index = 0; index < filters.size(); ++index) {
			final ItemCondition vo = (ItemCondition) filters.get(index);
			final int operator = Integer.parseInt(vo.getOperator());
			final String paramName = vo.getConditionField().replaceAll("[.]", "_") + index;
			param.put(paramName, vo.getValue());
			final QueryOperatorEnum operatorEnum = QueryOperatorEnum.valueOf(operator);
			switch (operatorEnum) {
				case EQUAL :
					sb.append(" and " + vo.getConditionField() + " = :" + paramName);
					break;
				case NOT_EQUAL :
					sb.append(" and " + vo.getConditionField() + " <> :" + paramName);
					break;
				case GREATER :
					sb.append(" and " + vo.getConditionField() + " > :" + paramName);
					break;
				case LESS :
					sb.append(" and " + vo.getConditionField() + " < :" + paramName);
					break;
				case GREATER_AND_EQUAL :
					sb.append(" and " + vo.getConditionField() + " >= :" + paramName);
					break;
				case LESS_AND_EQUAL :
					sb.append(" and " + vo.getConditionField() + " <= :" + paramName);
					break;
				case LIKE :
					final String paramValue = "%" + param.get(paramName) + "%";
					sb.append(" and " + vo.getConditionField() + " like :" + paramName);
					param.put(paramName, paramValue);
					break;
				case IN :
					sb.append(" and " + vo.getConditionField() + " in (:" + paramName + ")");
					break;
				case IS_NULL :
					sb.append(" and " + vo.getConditionField() + " is null");
					param.remove(paramName);
					break;
				case IS_NOT_NULL :
					sb.append(" and " + vo.getConditionField() + " is not null");
					param.remove(paramName);
					break;
				case NOT_IN :
					sb.append(" and " + vo.getConditionField() + " not in (:" + paramName + ")");
					break;
				case NOT_LIKE :
					final String paramValue2 = "%" + param.get(paramName) + "%";
					sb.append(" and " + vo.getConditionField() + " not like :" + paramName);
					param.put(paramName, paramValue2);
					break;
				default :
					break;
			} 
		} 
		option.setHql(sb.toString());
		option.setParam(param);
		return option;
	}	

}
